package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

type Announcement struct {
	ID        string `json:"id" gorm:"primaryKey;column:id;type:bigint;comment:唯一ID"`
	Name      string `json:"name" gorm:"column:name;type:varchar(30);comment:公告标题"`
	Content   string `json:"content" gorm:"column:content;type:text;comment:公告内容"`
	StartTime int64  `json:"start_time" gorm:"column:start_time;type:bigint;comment:结束时间"`
	EndTime   int64  `json:"end_time" gorm:"column:end_time;type:bigint;comment:结束时间"`
	Status    int    `json:"status,omitempty" gorm:"column:status;type:int"`
	CreatedAt int64  `json:"created_at" gorm:"column:created_at;autoCreateTime;type:int;size:64;index:idx_createdAt"`
}

func (Announcement) TableName() string {
	return "announcement"
}

// BeforeCreate 自增id
func (m *Announcement) BeforeCreate(tx *gorm.DB) (err error) {
	if m.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	m.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &Announcement{})
}
